package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.NewTypeModel;
import com.zeng.jiudian.bean.NewTypeModelExample;
import com.zeng.jiudian.bean.NewTypeModelExample.Criteria;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.dao.NewTypeDao;
import com.zeng.jiudian.mapper.NewTypeMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/8/13 - 14:15
 */
public class NewTypeDaoImpl implements NewTypeDao
{
    private SqlSession sqlSession;

    @Override
    public int add(NewTypeModel newTypeModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newTypeModel.setCreateTime(dateFormat.format(new Date()));
        newTypeModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            count = mapper.insert(newTypeModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<NewTypeModel> findAll(NewTypeModel newTypeModel)
    {
        List<NewTypeModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            NewTypeModelExample example = new NewTypeModelExample();
            Criteria criteria = example.createCriteria();
            if (newTypeModel.getTypeName() != null && !"".equals(newTypeModel.getTypeName()))
            {
                criteria.andTypeNameLike("%" + newTypeModel.getTypeName() + "%");
            }
            if (newTypeModel.getIsShow() != null && newTypeModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(newTypeModel.getIsShow());
            }
            if (newTypeModel.getRecommend() != null && newTypeModel.getRecommend() >= 0)
            {
                criteria.andIsShowEqualTo(newTypeModel.getRecommend());
            }

            if (!"无".equals(newTypeModel.getOrder()) && newTypeModel.getOrder() != null)
            {
                if ("创建时间".equals(newTypeModel.getOrder()))
                {
                    if ("降序".equals(newTypeModel.getLift()))
                    {
                        example.setOrderByClause("create_time desc");
                    }
                    else if ("升序".equals(newTypeModel.getLift()))
                    {
                        example.setOrderByClause("create_time asc");
                    }
                }
                else if ("更新时间".equals(newTypeModel.getOrder()))
                {
                    if ("降序".equals(newTypeModel.getLift()))
                    {
                        example.setOrderByClause("update_time desc");
                    }
                    else if ("升序".equals(newTypeModel.getLift()))
                    {
                        example.setOrderByClause("update_time asc");
                    }
                }
            }
            else
            {
                if ("降序".equals(newTypeModel.getLift()))
                {
                    example.setOrderByClause("id desc");
                }
                else if ("升序".equals(newTypeModel.getLift()))
                {
                    example.setOrderByClause("id asc");
                }
            }
            PageModel pageModel = newTypeModel.getPageModel();
            if (pageModel != null)
            {
                Integer nowNum = pageModel.getNowNum();
                Integer pageSize = pageModel.getPageSize();
                if (nowNum != null && pageSize != 0 && pageSize != null)
                {
                    example.setStartRow((nowNum - 1) * pageSize);
                    example.setPageSize(pageSize);
                }
            }
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(NewTypeModel newTypeModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            NewTypeModelExample example = new NewTypeModelExample();
            Criteria criteria = example.createCriteria();
            if (newTypeModel.getTypeName() != null && !"".equals(newTypeModel.getTypeName()))
            {
                criteria.andTypeNameLike("%" + newTypeModel.getTypeName() + "%");
            }
            if (newTypeModel.getIsShow() != null && newTypeModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(newTypeModel.getIsShow());
            }
            if (newTypeModel.getRecommend() != null && newTypeModel.getRecommend() >= 0)
            {
                criteria.andIsShowEqualTo(newTypeModel.getRecommend());
            }

            count = (int) mapper.countByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public NewTypeModel findById(int id)
    {
        NewTypeModel model = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            model = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }

    @Override
    public int delete(int id)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int update(NewTypeModel newTypeModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newTypeModel.setUpdateTime(dateFormat.format(new Date()));

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            count = mapper.updateByPrimaryKeySelective(newTypeModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<NewTypeModel> getAll()
    {
        List<NewTypeModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewTypeMapper mapper = sqlSession.getMapper(NewTypeMapper.class);
            list = mapper.selectByExample(null);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }
}
